package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Para;


import com.itheima.service.ParaService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.util.Map;

@RestController
@RequestMapping("/para")
public class ParaController {
    @Reference
    private ParaService paraService;

    //分页
    @RequestMapping("/findPage")
    public Result findPage(Integer page, Integer size, @RequestBody Map map){
        try{
            String name = (String) map.get("name");
            String options = (String) map.get("options");
            String id = (String)map.get("templateId");

            int templateId = Integer.parseInt(id);
            PageResult pageResult = paraService.findPage(page,size,name,options,templateId);
            return new Result(true,MessageConstant.QUERY_PARA_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PARA_FAIL);
        }

    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Para para){
        try{
            paraService.add(para);
        }catch(Exception e){
            return new Result(false, MessageConstant.ADD_PARA_FAIL);
        }
        return new Result(true,MessageConstant.ADD_PARA_SUCCESS);
    }

    //修改
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Para p = paraService.findById(id);
            return new Result(true,MessageConstant.QUERY_PARA_SUCCESS,p);
        }catch(Exception e){
            return new Result(false, MessageConstant.QUERY_PARA_FAIL);
        }

    }

    @RequestMapping("/update")
    public Result update(@RequestBody Para para){

        try{
            paraService.update(para);
        }catch (Exception e){
            System.out.println(e);
            return new Result(false,MessageConstant.EDIT_PARA_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_PARA_SUCCESS);
    }
    @RequestMapping("/delete")
    public Result del(Integer id){
        try{
            paraService.delete(id);
        }catch (Exception e){
            System.out.println(e);
            return new Result(false,MessageConstant. DELETE_PARA_FAIL);
        }
        return new Result(true,MessageConstant. DELETE_PARA_SUCCESS);
    }
}




